﻿using System;
using System.Windows;
using System.Windows.Controls;

using Perseus;
using Perseus.Data;

using Hetymine.Executioner.Data;

namespace Hetymine.Executioner.Controls {
    /// <summary>
    /// Interaction logic for BehaviourConfigurator.xaml
    /// </summary>
    public partial class BehaviourConfigurator : UserControl, IConfigurator<IniFile> {
        private IniFile _Config;
        private bool _IsApply = true;

        public BehaviourConfigurator() {
            InitializeComponent();

            this.cCommandBoxHotKey.TextChanged += new TextChangedEventHandler(Hotkey_TextChanged);
            this.cDefaultCommand.TextChanged += new TextChangedEventHandler(DefaultCommand_TextChanged);
            this.cEnableSpeech.Click += new RoutedEventHandler(Check_Click);
            this.cShowNotificationIcon.Click += new RoutedEventHandler(Check_Click);
            this.cIconSingleClick.SelectionChanged += new SelectionChangedEventHandler(IconClick_SelectionChanged);
            this.cIconDoubleClick.SelectionChanged += new SelectionChangedEventHandler(IconClick_SelectionChanged);
            this.cShowInTaskbar.Click += new RoutedEventHandler(Check_Click);
            this.cSaveHistory.Click += new RoutedEventHandler(SaveHistory_Click);
            this.cMaxHistoryItems.TextChanged += new TextChangedEventHandler(MaxHistoryItems_TextChanged);
            this.cDisplay.SelectionChanged += new SelectionChangedEventHandler(Display_SelectionChanged);
        }

        private void Hotkey_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetString("Behaviour", "Command Box Hot Key", this.cCommandBoxHotKey.Text);
        }
        private void DefaultCommand_TextChanged(object sender, TextChangedEventArgs e) {
            if (!this._IsApply) { return; }

            this._Config.SetString("Behaviour", "Default Command", this.cDefaultCommand.Text);
        }
        private void IconClick_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            ComboBox cb = sender as ComboBox;
            int index = cb.SelectedIndex;

            switch (cb.Name) {
                case "cIconSingleClick":
                    this._Config.SetInt("Behaviour", "Notification Icon Single Click", index);
                    break;
                case "cIconDoubleClick":
                    this._Config.SetInt("Behaviour", "Notification Icon Double Click", index);
                    break;
            }
        } 
        private void Check_Click(object sender, RoutedEventArgs e) {
            if (!this._IsApply) { return; }

            CheckBox cb = sender as CheckBox;

            switch (cb.Name) {
                case "cEnableSpeech":
                    this._Config.SetBool("Behaviour", "Enable Speech", cb.IsChecked.Value);
                    break;
                case "cShowNotificationIcon":
                    this._Config.SetBool("Behaviour", "Show Notification Icon", cb.IsChecked.Value);
                    break;
                case "cShowInTaskbar":
                    this._Config.SetBool("Behaviour", "Show In Taskbar", cb.IsChecked.Value);
                    break;
            }
        }
        private void SaveHistory_Click(object sender, RoutedEventArgs e) {
            this._Config.SetBool("Behaviour", "Save History", cSaveHistory.IsChecked.Value);
        }
        private void MaxHistoryItems_TextChanged(object sender, TextChangedEventArgs e) {
            int items;
            if (!int.TryParse(this.cMaxHistoryItems.Text, out items)) {
                items = 250;
            }
            items = Math.Max(-1, items);

            this._Config.SetInt("Behaviour", "Max History Items", items);
        }
        private void Display_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (!this._IsApply) { return; }

            int display = this.cDisplay.SelectedIndex - 1;
            this._Config.SetInt("Behaviour", "Display", display);
        }

        private int DisplayIndex() {
            int monitor = this._Config.GetInt("Behaviour", "Display", -1);

            if (monitor >= 0 && monitor < System.Windows.Forms.Screen.AllScreens.Length) {
                return monitor + 1;
            }

            return 0;
        }

        #region IConfigurator<IniFile> Members
        public void InitializeConfig(IniFile config) {
            this._Config = config;

            this._IsApply = false;

            // Command Options
            this.cCommandBoxHotKey.Text = this._Config.GetString("Behaviour", "Command Box Hot Key", "Alt + C");
            this.cDefaultCommand.Text = this._Config.GetString("Behaviour", "Default Command", string.Empty);
            this.cEnableSpeech.IsChecked = this._Config.GetBool("Behaviour", "Enable Speech", false);

            // Notification Icon
            this.cShowNotificationIcon.IsChecked = this._Config.GetBool("Behaviour", "Show Notification Icon", PEnvironment.IsWindowsVistaOrBelow);
            int iconClick = this._Config.GetInt("Behaviour", "Notification Icon Single Click", 2);
            iconClick = iconClick.MaxMin(0, 4);
            this.cIconSingleClick.SelectedIndex = iconClick;
            iconClick = this._Config.GetInt("Behaviour", "Notification Icon Double Click", 1);
            iconClick = iconClick.MaxMin(0, 4);
            this.cIconDoubleClick.SelectedIndex = iconClick;

            // Taskbar
            this.cShowInTaskbar.IsChecked = this._Config.GetBool("Behaviour", "Show In Taskbar", PEnvironment.IsWindows7OrAbove);

            // Command Histroy
            this.cSaveHistory.IsChecked = this._Config.GetBool("Behaviour", "Save History", true);
            this.cMaxHistoryItems.Text = this._Config.GetInt("Behaviour", "Max History Items", 250).ToString();

            // Multiple Displays
            ComboBoxItem cbi = new ComboBoxItem();
            System.Windows.Forms.Screen prim = System.Windows.Forms.Screen.PrimaryScreen;
            cbi.Content = "Primary Display" + " (" + prim.Bounds.Width.ToString() + "×" + prim.Bounds.Height.ToString() + ")";
            this.cDisplay.Items.Add(cbi);
            int display = 0;
            foreach (System.Windows.Forms.Screen s in System.Windows.Forms.Screen.AllScreens) {
                ++display;
                cbi = new ComboBoxItem();
                cbi.Content = "Display " + display.ToString() + " (" + s.Bounds.Width.ToString() + "×" + s.Bounds.Height.ToString() + ")";
                this.cDisplay.Items.Add(cbi);
            }

            this.cDisplay.SelectedIndex = DisplayIndex();

            if (System.Windows.Forms.Screen.AllScreens.Length <= 1) {
                this.cDisplayGroup.Visibility = Visibility.Collapsed;
            }

            this._IsApply = true;
        }
        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
